﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Security.Permissions;
using System.IO;

namespace DirectorySynchronizer
{
    [SecurityPermission(SecurityAction.LinkDemand)]
    public sealed partial class HTMLDisplayForm : Form
    {
        private bool donotnavigate = true;
        private Screen targetscreen = null;
        private MainForm parentoptionsform = null;
        private wbc iwbc = null;
        private string htmltext = null;
        private string outputstring = null;
        private string replacementtext = null;

        public HTMLDisplayForm()
        {
            InitializeComponent();
            iwbc = new wbc(this);
            webBrowserHTMLDisplay.ObjectForScripting = iwbc;
        }
        public HTMLDisplayForm(string argResourceName, Screen argScreen, MainForm argParentForm): this()
        {
            htmltext = HTMLDisplayForm.GetDocumentationHTMLString(argResourceName);
            targetscreen = argScreen;
            parentoptionsform = argParentForm;
        }

        public void SetReplacementText(string argString)
        {
            donotnavigate = true;
            htmltext = htmltext.Replace("$REPLACEMENTTEXT$", argString);
            replacementtext = argString;
        }

        private void RTFDisplayForm_Load(object sender, EventArgs e)
        {
            if (htmltext == null)
            {
                MessageBox.Show("Error loading documentation, please contact On-Space Software Incorporated for support.", "KeyboClick v1.0: Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
            if (targetscreen != null)
            {
                Left = targetscreen.Bounds.Left + 15;
                Top = targetscreen.Bounds.Top + 15;
                Height = targetscreen.Bounds.Height - 60;
            }
            webBrowserHTMLDisplay.DocumentText = htmltext;
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            if (parentoptionsform != null)
            {
                parentoptionsform.ShowOptionsFormAdjacentTo(this);
                this.Focus();
            }
        }
        private void webBrowserHTMLDisplay_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (donotnavigate)
            {
                donotnavigate = false;
                return;
            }
            Process.Start(e.Url.ToString());
            e.Cancel = true;
        }
        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void HTMLDisplayForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (parentoptionsform != null)
            {
                parentoptionsform.ChildHTMLDisplayFormClosed();
            }
        }


        [ComVisible(true)]
        public class wbc
        {
            private HTMLDisplayForm ParentHTMLDisplayForm = null;

            public wbc(HTMLDisplayForm argParentHTMLDisplayForm)
            {
                ParentHTMLDisplayForm = argParentHTMLDisplayForm;
            }

            [ObfuscationAttribute(Exclude = true)]
            public void sh(string argString)
            {
                //the license code is imported here
                //don't forget to get user confirm
                ParentHTMLDisplayForm.outputstring = argString;
                MessageBox.Show(ParentHTMLDisplayForm.outputstring);
            }

            [ObfuscationAttribute(Exclude = true)]
            public void cl()
            {
                Clipboard.SetText(ParentHTMLDisplayForm.replacementtext);
            }
        }

        private static string GetDocumentationHTMLString(string argResourceName)
        {
            try
            {
                string returnstring = null;
                System.Reflection.Assembly exeasm = System.Reflection.Assembly.GetExecutingAssembly();
                using (StreamReader reader = new StreamReader(exeasm.GetManifestResourceStream(argResourceName)))
                {
                    returnstring = reader.ReadToEnd();
                }
                return returnstring;
            }
            catch
            {
                return null;
            }
        }
    }
}
